﻿Imports System.Data.SqlClient

Public Class deckDriver
    Inherits System.Web.UI.Page
    Dim objDB As New DBConnect
    Dim ds As New DataSet
    Dim dr As SqlDataReader
    Dim card As New Card
    Shared deck As New Deck
    Shared hand As New Deck

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        objDB = Session("db")
    End Sub

    Protected Sub btnDeck_Click(sender As Object, e As EventArgs) Handles btnDeck.Click
        Dim sql = "SELECT Card.CardID, Card.CardCategoryID, Card.CardName, Card.CardInstruction, Card.CardDesc FROM Game, Game_Card, card, CardCategory WHERE Game.GameID = Game_Card.GameID And Game_Card.CardID = card.CardID And card.CardCategoryID = CardCategory.CardCategoryID And Game.GameID = '12'"

        ds = objDB.getDataSet(sql)

        Dim numOfCards = ds.Tables(0).Rows.Count()

        gvDeck.DataSource = ds
        gvDeck.DataBind()

        If deck.hasCards Then
            deck.removeAllCards()
            hand.removeAllCards()
            Label4.Text = ""
            Label5.Text = ""
        End If

        For i As Integer = 0 To (numOfCards - 1)
            Dim instruction As String
            Dim description As String

            If IsDBNull(ds.Tables(0).Rows(i)("CardInstruction")) Then
                instruction = ""
            Else
                instruction = ds.Tables(0).Rows(i)("CardInstruction")
            End If

            If IsDBNull(ds.Tables(0).Rows(i)("CardDesc")) Then
                description = ""
            Else
                description = ds.Tables(0).Rows(i)("CardDesc")
            End If

            card = New Card(ds.Tables(0).Rows(i)("CardID"), ds.Tables(0).Rows(i)("CardCategoryID"), ds.Tables(0).Rows(i)("CardName"), instruction, description)
            deck.Add(card)
        Next

        Label2.Text = "Gridview to display SQL Datasource retrieved from database"
        Label3.Text = "Number of cards in deck: " & numOfCards & "<br />"
        Label1.Text = "<b>DECK OBJECT CONTAINING CARD OBJECTS:</b><br />" & deck.ToString

    End Sub

    Protected Sub btnShuffle_Click(sender As Object, e As EventArgs) Handles btnShuffle.Click
        deck.Shuffle()

        Label1.Text = "<b>SHUFFLED DECK OBJECT CONTAINING CARD OBJECTS:</b><br />" & deck.ToString
    End Sub

    Protected Sub btnDeal_Click(sender As Object, e As EventArgs) Handles btnDeal.Click
        If deck.hasCards() Then
            hand.Add(deck.Deal())

            Label1.Text = "<b>DECK OBJECT CONTAINING CARD OBJECTS:</b><br />" & deck.ToString
            Label4.Text = "<b>PLAYER HAND CONTAINING CARD OBJECTS:</b><br />" & hand.ToString
        Else
            Label5.Text = "Sorry, there are no more cards to deal."
        End If


    End Sub
End Class